var save_max_len=20;
var display_max_len=15;


$(document).ready(function() {
	$("#number").click(function(){
		stopbubble(arguments[0]);
		var obj = document.getElementById("history");
		if(obj.style.display=="none"){
            obj.style.display = "";  
        } else{
            
        }
		showHistory();
    });
	
	document.onclick = function(){
		var obj = document.getElementById("history");
            if(obj.style.display==""){
                obj.style.display = "none";  
            } else{
                
            }
	}
    
    
});



//��ʾ��ʷ��¼
function showHistory(){
	var data = new Array();
	var cookie = $.cookie("search_history");//��ȡcookie
	if(cookie!=null){
		data = JSON.parse(cookie);//��cookie��ȡ������
	}
	$("#history").empty();//���ԭ������ʾ����
	
	var str = $("#number").val();
	if(!str){
		str = "";
	}
	if(data!=null){
		var len = data.length>display_max_len?display_max_len:data.length;//��ʾʱֻ��ʾ�ض�������
		var limit = data.length-len-1;
		for (var i = data.length-1; i >limit ; i--) {
            if(data[i].indexOf(str)>(-1)){//��̬������ʷ��¼��Ŀ
                var liHtml = '<li>'+data[i]+'</li>';
                $("#history").append(liHtml);
				
				$("#history li").off().on('click', function(){
		            $("#number").val($(this).text());
					var obj = document.getElementById("history");
                    if(obj.style.display==""){
                        obj.style.display = "none";  
                    } else{
                        
                    }
					
		        });
            }
			
        }
	}
	
}




function stopbubble(e){
    e.stopPropagation();//stop bubble event on this node 
        }

function toSearch(){
	var number = $("#number").val();
	if(number=="" || number=="请输入“料号”或“图号”!"){
		return;
	}
	addHistory(number);
}

	
//��Ӽ�¼
function addHistory(str){
	var data = new Array();
	var cookie = $.cookie("search_history");
	if(cookie!=null){
		data = JSON.parse(cookie);
	}
	
	//�����ʷ��¼���У�����ɾ����Ȼ������ӣ�������������ļ�¼�����£�������ֱ�����
	var index=-1;
    if(data){
		
		if (!Array.prototype.indexOf){
        Array.prototype.indexOf = function(elt /*, from*/){
        var len = this.length >>> 0;
        var from = Number(arguments[1]) || 0;
        from = (from < 0)
             ? Math.ceil(from)
             : Math.floor(from);
        if (from < 0)
          from += len;
        for (; from < len; from++)
        {
          if (from in this &&
              this[from] === elt)
            return from;
        }
        return -1;
      };
    }

        index=data.indexOf(str);
    }
	if(index>-1){
		data.splice(index,1);//ɾ��ԭ����
	}
	
	//��ౣ��save_max_len����¼������֮��ɾ����һ��
	if(data && data.length==save_max_len){
		data.splice(0,1);
	}
	data.push(str);
	$.cookie('search_history', JSON.stringify(data), {expires : 30});//������Ч��
}

	
